﻿using SmartPlant.Core.Mvvm;
using System.Collections;
using System.Collections.Specialized;
using System.Windows;
using Telerik.Windows.Controls;

namespace SmartPlant.Core.Behavior
{
    /// <summary>
    /// 用于支持MVVM模式下绑定GroupDescriptors属性
    /// </summary>
    public class TelerikGroupDescriptorBindingBehavior : SmartViewModel
    {
        private readonly RadGridView grid = null;
        private readonly INotifyCollectionChanged _groupColumns = null;

        public static readonly DependencyProperty GroupDescriptorsProperty
            = DependencyProperty.RegisterAttached("GroupDescriptors", typeof(INotifyCollectionChanged), typeof(TelerikGroupDescriptorBindingBehavior),
                new PropertyMetadata(new PropertyChangedCallback(OnGroupDescriptorsPropertyChanged)));

        public static void SetGroupDescriptors(DependencyObject dependencyObject, INotifyCollectionChanged columns)
        {
            dependencyObject.SetValue(GroupDescriptorsProperty, columns);
        }

        public static INotifyCollectionChanged GetGroupDescriptors(DependencyObject dependencyObject)
        {
            return (INotifyCollectionChanged)dependencyObject.GetValue(GroupDescriptorsProperty);
        }

        private static void OnGroupDescriptorsPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var grid = dependencyObject as RadGridView;
            var columns = e.NewValue as INotifyCollectionChanged;

            if (grid != null && columns != null)
            {
                var behavior = new TelerikGroupDescriptorBindingBehavior(grid, columns);
                behavior.Attach();
            }
        }

        private void Attach()
        {
            if (grid != null && _groupColumns != null)
            {
                Transfer(GetGroupDescriptors(grid) as IList, grid.GroupDescriptors);

                _groupColumns.CollectionChanged -= ContextGroupDescriptors_CollectionChanged;
                _groupColumns.CollectionChanged += ContextGroupDescriptors_CollectionChanged;
            }
        }

        public TelerikGroupDescriptorBindingBehavior(RadGridView grid, INotifyCollectionChanged columns)
        {
            this.grid = grid;
            this._groupColumns = columns;
        }

        void ContextGroupDescriptors_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UnsubscribeFromEvents();

            Transfer(GetGroupDescriptors(grid) as IList, grid.GroupDescriptors);

            SubscribeToEvents();
        }

        void GridGroupDescriptors_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            UnsubscribeFromEvents();

            Transfer(grid.GroupDescriptors, GetGroupDescriptors(grid) as IList);

            SubscribeToEvents();
        }

        private void SubscribeToEvents()
        {
            grid.GroupDescriptors.CollectionChanged += GridGroupDescriptors_CollectionChanged;

            if (GetGroupDescriptors(grid) != null)
            {
                GetGroupDescriptors(grid).CollectionChanged += ContextGroupDescriptors_CollectionChanged;
            }
        }

        private void UnsubscribeFromEvents()
        {
            grid.GroupDescriptors.CollectionChanged -= GridGroupDescriptors_CollectionChanged;

            if (GetGroupDescriptors(grid) != null)
            {
                GetGroupDescriptors(grid).CollectionChanged -= ContextGroupDescriptors_CollectionChanged;
            }
        }

        public static void Transfer(IList source, IList target)
        {
            if (source == null || target == null)
                return;

            target.Clear();

            foreach (object o in source)
            {
                target.Add(o);
            }
        }
    }
}
